package util

import (
	"bytes"
	"io/ioutil"
	"unicode/utf8"

	"golang.org/x/text/encoding/simplifiedchinese"
	"golang.org/x/text/transform"
)

func IsUTF8(s []byte) bool {
	return utf8.Valid(s)
}

func GbkToUTF8(gbkBytes []byte) ([]byte, error) {
	reader := transform.NewReader(bytes.NewReader(gbkBytes), simplifiedchinese.GBK.NewDecoder())
	utf8Bytes, err := ioutil.ReadAll(reader)
	if err != nil {
		return nil, err
	}
	return utf8Bytes, nil
}
